/**
 * 
 */
package com.nervytech.tutorcruise.spring.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nervytech.tutorcruise.db.service.interfaces.ICompanyProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IInstituteProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IUserService;
import com.nervytech.tutorcruise.profile.beans.builders.IProfileBeanBuilder;
import com.nervytech.tutorcruise.service.exception.TrainerCruiseServiceException;
import com.nervytech.tutorcruise.spring.security.auth.SessionUser;
import com.nervytech.tutorcruise.spring.security.auth.UserDetailsServiceImpl;

/**
 * @author ADMIN
 * 
 */
@Controller
@RequestMapping("/usr/profile")
public class ProfileController {

	private static final String BEAN_BUILDER_CLASS_NAME_SUFFIX = "ProfileBeanBuilder";

	@Autowired
	private BeanFactory beanFactory;

	@Autowired
	private IUserService userService;

	@Autowired
	private IInstituteProfileService instituteProfileService;

	@Autowired
	private ICompanyProfileService companyProfileService;

	@RequestMapping(method = RequestMethod.GET)
	public String showProfile(Map model) throws TrainerCruiseServiceException {

		String toReturn = null;

		SessionUser user = UserDetailsServiceImpl.currentUserDetails();

		System.out.println("Inside Show Profileeeeeeeeeeeeeeeeeeee");

		IProfileBeanBuilder beanBuilder = getProfileBeanBuilder(user
				.getAuthorities());

		//Object usrBean = beanBuilder.getProfileBean(user.getUserId());
		
		List<GrantedAuthority> userRoles = collectionToList(user.getAuthorities());
		GrantedAuthority gau = (GrantedAuthority) userRoles.get(0);

		String userType = gau.getAuthority();
		
		model.put(userType.toLowerCase()+"ProfileBean", beanBuilder.getProfileBeanG(user.getUserId()));
		toReturn = userType.toLowerCase()+"Profile";

		/*if (beanBuilder instanceof TraineeProfileBeanBuilder) {
			model.put("traineeProfileBean", (UserBean) usrBean);
			toReturn = "traineeProfile";
		} else if (beanBuilder instanceof TrainerProfileBeanBuilder) {
			System.out
					.println("Inside Trainer Profile Bean BuilderRRRRRRRRRRR ");
			model.put("trainerProfileBean", (TrainerProfileBean) usrBean);
			toReturn = "trainerProfile";
		} else if (beanBuilder instanceof InstituteProfileBeanBuilder) {
			model.put("instituteProfileBean", (InstituteProfileBean) usrBean);
			toReturn = "instituteProfile";
		} else if (beanBuilder instanceof CompanyProfileBeanBuilder) {
			model.put("companyProfileBean", (CompanyProfileBean) usrBean);
			toReturn = "companyProfile";
		}*/

		return toReturn;
	}

	private IProfileBeanBuilder getProfileBeanBuilder(
			Collection<GrantedAuthority> collection) {

		IProfileBeanBuilder beanBuilder = null;

		List<GrantedAuthority> userRoles = collectionToList(collection);
		GrantedAuthority gau = (GrantedAuthority) userRoles.get(0);

		String userType = gau.getAuthority();

		return beanFactory.getBean(userType.toLowerCase()
				+ BEAN_BUILDER_CLASS_NAME_SUFFIX, IProfileBeanBuilder.class);

		/*
		 * if (userType.equalsIgnoreCase(UserTypeEnum.TRAINER.name())) {
		 * beanBuilder = new TrainerProfileBeanBuilder(); } else if
		 * (userType.equalsIgnoreCase(UserTypeEnum.TRAINEE.name())) {
		 * beanBuilder = new TraineeProfileBeanBuilder(); } else if
		 * (userType.equalsIgnoreCase(UserTypeEnum.INSTITUTE.name())) {
		 * beanBuilder = new InstituteProfileBeanBuilder(); } else if
		 * (userType.equalsIgnoreCase(UserTypeEnum.COMPANY.name())) {
		 * beanBuilder = new CompanyProfileBeanBuilder(); }
		 * 
		 * return beanBuilder;
		 */
	}

	public <E> List<E> collectionToList(Collection<E> collection) {
		List<E> list;
		if (collection instanceof List) {
			list = (List<E>) collection;
		} else {
			list = new ArrayList<E>(collection);
		}
		return list;
	}

}
